<%--
Copyright (C) 2008 Jackson Harper (jackson@ximian.com)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
--%>

<%@ Control Language="C#" ClassName="WYSIWYGControl" %>
<%@ Register Assembly="Gaia.WebWidgets" Namespace="Gaia.WebWidgets" TagPrefix="gaia" %>
<%@ Register Assembly="Gaia.WebWidgets.Extensions" Namespace="Gaia.WebWidgets.Extensions" TagPrefix="cc1" %>

<script runat="server">

    public string Text
    {
        get
        {
            return (InComposeMode() ? CleanupHtml (ComposeRichEdit.Text) : CleanupHtml (EditHtmlTextBox.Text));
        }
        set
        {
            if (InComposeMode())
                ComposeRichEdit.Text = value;
            else
                EditHtmlTextBox.Text = value;
        }
    }

    protected void EditorMultiView_ActiveViewChanged(object sender, EventArgs e)
    {
        ControlsPanel.Visible = (EditorMultiView.ActiveViewIndex == 0);
    }

    protected void ViewToggleButton_Click(object sender, EventArgs e)
    {
        if (EditorMultiView.ActiveViewIndex == 0)
        {
            string text = ComposeRichEdit.Text;
            ViewToggleButton.Text = "[Compose]";
            EditorMultiView.ActiveViewIndex = 1;
            EditorMultiView.ForceAnUpdate();
            EditHtmlTextBox.Text = CleanupHtml (text);
        }
        else
        {
            string text = EditHtmlTextBox.Text;
            ViewToggleButton.Text = "[Edit HTML]";
            EditorMultiView.ActiveViewIndex = 0;
            EditorMultiView.ForceAnUpdate();
            ComposeRichEdit.Text = CleanupHtml (text);
        }
    }

    private string CleanupHtml(string str)
    {
        // It would be a good idea to ensure that no scripts or anything are injected here
        
        // Don't let people go <br> crazy at the end of the text
        while (str.Length > 0)
        {
            if (str.EndsWith("<br>"))
                str = str.Substring(0, str.Length - "<br>".Length);
            else
                break;          
        }

        return Grr.Model.HtmlCleaner.CleanHtml (str);     
    }

    protected void BoldButton_Click(object sender, ImageClickEventArgs e)
    {
        ComposeRichEdit.EditBold();
        ComposeRichEdit.Focus();
    }

    protected void ItalicButton_Click(object sender, ImageClickEventArgs e)
    {
        ComposeRichEdit.EditItalic();
        ComposeRichEdit.Focus();
    }

    protected void UnderlineButton_Click(object sender, ImageClickEventArgs e)
    {
        ComposeRichEdit.EditUnderline();
        ComposeRichEdit.Focus();
    }

    protected void LinkButton_Click(object sender, ImageClickEventArgs e)
    {
        AddLinkWindow.Visible = true;
        AddLinkTextTextBox.Text = ComposeRichEdit.SelectedText;
        AddLinkUrlTextBox.Text = "http://";
    }

    protected void CancelAddLinkLinkButton_Click(object sender, EventArgs e)
    {
        AddLinkWindow.Visible = false;
    }

    protected void AddLinkSubmitButton_Click(object sender, EventArgs e)
    {
        ComposeRichEdit.PasteHtml (String.Format ("<a href=\"{0}\">{1}</a> ", AddLinkUrlTextBox.Text, AddLinkTextTextBox.Text));
        AddLinkWindow.Visible = false;
    }

    protected void SaveButton_Click(object sender, EventArgs e)
    {
        RaiseBubbleEvent(this, new Grr.Web.WYSIWYGCommandEventArgs(Grr.Web.WYSIWYGCommand.Save));
    }

    private bool InComposeMode()
    {
        return (EditorMultiView.ActiveViewIndex == 0);
    }

    protected void CancelButton_Click(object sender, EventArgs e)
    {
        RaiseBubbleEvent(this, new Grr.Web.WYSIWYGCommandEventArgs(Grr.Web.WYSIWYGCommand.Cancel));
    }
</script>

<table bgcolor="#DDE673" cellspacing="5">
    <tr>
        <td height="35">
            <table width="100%">
                <tr>
                    <td>
                        <gaia:Panel ID="ControlsPanel" runat="server">
                            <gaia:ImageButton ID="BoldButton" ImageUrl="../media/wysiwyg/format-text-bold.PNG" runat="server" OnClick="BoldButton_Click" />
                            <gaia:ImageButton ID="ItalicButton" ImageUrl="../media/wysiwyg/format-text-italic.PNG" runat="server" OnClick="ItalicButton_Click" />
                            <gaia:ImageButton ID="UnderlineButton" ImageUrl="../media/wysiwyg/format-text-underline.PNG" runat="server" OnClick="UnderlineButton_Click" />
                            <gaia:ImageButton ID="LinkButton" ImageUrl="~/media/wysiwyg/applications-internet.png" runat="server" OnClick="LinkButton_Click" />
                        </gaia:Panel>
                    </td>
                    <td align="right">
                        <gaia:LinkButton ID="ViewToggleButton" Text="[Edit HTML]" runat="server" OnClick="ViewToggleButton_Click"></gaia:LinkButton>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td style="background-color:White;">
            <gaia:MultiView ID="EditorMultiView" ActiveViewIndex="0" runat="server" OnActiveViewChanged="EditorMultiView_ActiveViewChanged">

                <asp:View ID="ComposeView" runat="server">
                    <span style="background-color:White;"><gaia:RichEdit ID="ComposeRichEdit" Height="400" Width="760"  runat="server" /></span>
                </asp:View>
                
                <asp:View ID="EditHtmlView" runat="server">
                    <gaia:TextBox ID="EditHtmlTextBox" TextMode="MultiLine" Height="400" Width="760" runat="server" style="border: 0px"/>
                </asp:View>
                
            </gaia:MultiView>
        </td>
    </tr>
    <tr>
        <td align="right" valign="middle">
            <gaia:LinkButton ID="CancelButton" Text="[Cancel]" runat="server" OnClick="CancelButton_Click" CssClass="ControlLinkButton"></gaia:LinkButton>
            <gaia:LinkButton ID="SaveButton" Text="[Save]" runat="server" OnClick="SaveButton_Click" CssClass="ControlLinkButton"></gaia:LinkButton>
        </td>
    </tr>
</table>

<gaia:Window ID="AddLinkWindow"
        Width="300px"
        CssClass="light_green" 
        AutoCenter="true" 
        CanClickEscapeToClose="true" 
        Closable="true" 
        Draggable="false" 
        Maximizable="false" 
        Minimizable="false" 
        Modal="true" 
        Resizable="false" 
        ScaleHeightToContent="true"
        Visible="false"
        runat="server" 
        HeightOfBorders="28" 
        WidthOfBorders="12">
        
        <div align="center" style="margin:15,15,15,15;">
            <table>
                <tr>
                    <td colspan="2" align="center" valign="middle">Add a link</td>
                </tr>
                <tr>
                    <td align="center" colspan="2">&nbsp;</td>
                </tr>
                <tr>
                    <td align="right">Url:</td>
                    <td><gaia:TextBox ID="AddLinkUrlTextBox" runat="server"></gaia:TextBox></td>
                </tr>
                <tr>
                    <td align="right">Text:</td>
                    <td><gaia:TextBox ID="AddLinkTextTextBox" TextMode="SingleLine" runat="server"></gaia:TextBox></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>
                        <gaia:Button ID="AddLinkSubmitButton" Text="Add link" runat="server" OnClick="AddLinkSubmitButton_Click"/></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td valign="bottom">
                        <gaia:LinkButton ID="CancelAddLinkLinkButton" runat="server" OnClick="CancelAddLinkLinkButton_Click">Cancel</gaia:LinkButton>
                    </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
            </table>
        </div>
        
</gaia:Window>

